import wsgiref.handlers

from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp


class DBUser(db.Model):
  user = db.UserProperty()
  nickname = db.StringProperty(multiline=False)
  nickname_set = db.BooleanProperty()


class MainPage(webapp.RequestHandler):
  def get(self):
#    user = users.get_current_user()

#    if user:
    self.response.headers['Content-Type'] = 'text/html'
    self.response.out.write("""<html>
 <head>
  <meta name="verify-v1" content="dKwCNN3NrzBV7UMXgHu8RYp/r+H+ZjC+HWZl5TrFOwc=" />
  <title>Ricochet Robots 2.0</title>
  <style>
body {
  font-family: sans-serif;
  color: #cccccc;
  background-color: #000000;
}
a {
  color: #cccc00;
}
#title {
  font-size: 24px;
  color: #cccc00;
  width: 100%;
  text-align: center;
  padding: 0px 0px 25px 0px;
}
#board {
}
#gape {
  position: fixed;
  bottom: 5px;
  right: 5px;
}
.row {
  margin: 0 auto;
}
.cell {
  font-size: 2px;
  background-color: #c0c0c0;
  border-left: 2px solid #a0a0a0;
  border-top: 2px solid #a0a0a0;
  border-right: 2px solid #808080;
  border-bottom: 2px solid #808080;
  padding: 11px 12px 11px 12px;
}
.center {
  color: #a0a0a0;
  background-color: #a0a0a0;
}
.corner_tl {
}
  </style>
  <script language="Javascript">

function showEmail(email, name, shift) {
  var result = "";

  for(var index = 0; index < email.length; index++) {
    result += String.fromCharCode(email.charCodeAt(index) + shift);
  }

  document.write('<a href="mailto:' + result + '">' + name + '</a>');
}

function hWall(quadrant, row, col) {
  if(quadrant == 1) {
    r = 9 - row;
    c = 8 + col;
    document.getElementById("r" + r + "c" + c).style.borderBottom = "2px solid #000000";
  }
  if(quadrant == 2) {
    r = 9 - col;
    c = 9 - row;
    document.getElementById("r" + r + "c" + c).style.borderRight = "2px solid #000000";
  }
  if(quadrant == 3) {
    r = 8 + row;
    c = 9 - col;
    document.getElementById("r" + r + "c" + c).style.borderTop = "2px solid #000000";
  }
  if(quadrant == 4) {
    r = 8 + col;
    c = 8 + row;
    document.getElementById("r" + r + "c" + c).style.borderLeft = "2px solid #000000";
  }
}

function vWall(quadrant, row, col) {
  if(quadrant == 1) {
    r = 9 - row;
    c = 8 + col;
    document.getElementById("r" + r + "c" + c).style.borderLeft = "2px solid #000000";
  }
  if(quadrant == 2) {
    r = 9 - col;
    c = 9 - row;
    document.getElementById("r" + r + "c" + c).style.borderBottom = "2px solid #000000";
  }
  if(quadrant == 3) {
    r = 8 + row;
    c = 9 - col;
    document.getElementById("r" + r + "c" + c).style.borderRight = "2px solid #000000";
  }
  if(quadrant == 4) {
    r = 8 + col;
    c = 8 + row;
    document.getElementById("r" + r + "c" + c).style.borderTop = "2px solid #000000";
  }
}

function prepCells() {
  document.getElementById("r8c8").className = "center";
  document.getElementById("r8c9").className = "center";
  document.getElementById("r9c8").className = "center";
  document.getElementById("r9c9").className = "center";
  for(pos = 1; pos <= 8; pos++) {
    vWall(1,pos,-7);
    hWall(1,-7,pos);
    vWall(2,pos,-7);
    hWall(2,-7,pos);
    vWall(3,pos,-7);
    hWall(3,-7,pos);
    vWall(4,pos,-7);
    hWall(4,-7,pos);
  }
  vWall(4,1,0);
  hWall(2,0,1);
  vWall(1,1,0);
  hWall(3,0,1);
  vWall(1,0,0);
  hWall(1,0,0);
  hWall(1,0,1);
  vWall(3,1,0);
  hWall(1,Math.floor(Math.random() * 6) + 2,8);
  vWall(1,8,Math.floor(Math.random() * 6) + 2);
  hWall(2,Math.floor(Math.random() * 6) + 2,8);
  vWall(2,8,Math.floor(Math.random() * 6) + 2);
  hWall(3,Math.floor(Math.random() * 6) + 2,8);
  vWall(3,8,Math.floor(Math.random() * 6) + 2);
  hWall(4,Math.floor(Math.random() * 6) + 2,8);
  vWall(4,8,Math.floor(Math.random() * 6) + 2);
}

function placeRobot(color) {

}
  </script>
 </head>
 <body>
  <div id="title">
   Ricochet Robots 2.0
  </div>
  <div>
   Welcome to Ricochet Robots 2.0, %s.  This site is currently under development.
   <p />
   When Ricochet Robots 2.0 is live, you will be able to challenge your friends to a Web 2.0 version of Ricochet Robots.  The application will work using Google App Engine and the Google Web Toolkit in order to prevent the server issues that brought Ricochet Robots 1.0's demise.
   <p />
   When Ricochet Robots 2.5 is live, you will be able to play the Ricochet Robots daily challenge, using the same interface you've become familiar with in Ricochet Robots 2.0.
   <p />
   If you are interested in contributing to the development of either of these Ricochet Robots projects, please e-mail <script language="Javascript">showEmail("absqb^j=of`l`ebqol_lq+`lj", "our development team", 3);</script><noscript>--You must have JavaScript enabled to use this site--</noscript>.
   <p />
   Anticipated launch date: January 1, 2010
  </div>
  <table id="board" align="center" cellspacing="0">
   <tr id="r1" class="row">
    <td id="r1c1" class="cell">.</td>
    <td id="r1c2" class="cell">.</td>
    <td id="r1c3" class="cell">.</td>
    <td id="r1c4" class="cell">.</td>
    <td id="r1c5" class="cell">.</td>
    <td id="r1c6" class="cell">.</td>
    <td id="r1c7" class="cell">.</td>
    <td id="r1c8" class="cell">.</td>
    <td id="r1c9" class="cell">.</td>
    <td id="r1c10" class="cell">.</td>
    <td id="r1c11" class="cell">.</td>
    <td id="r1c12" class="cell">.</td>
    <td id="r1c13" class="cell">.</td>
    <td id="r1c14" class="cell">.</td>
    <td id="r1c15" class="cell">.</td>
    <td id="r1c16" class="cell">.</td>
   </tr>
   <tr id="r2" class="row">
    <td id="r2c1" class="cell">.</td>
    <td id="r2c2" class="cell">.</td>
    <td id="r2c3" class="cell">.</td>
    <td id="r2c4" class="cell">.</td>
    <td id="r2c5" class="cell">.</td>
    <td id="r2c6" class="cell">.</td>
    <td id="r2c7" class="cell">.</td>
    <td id="r2c8" class="cell">.</td>
    <td id="r2c9" class="cell">.</td>
    <td id="r2c10" class="cell">.</td>
    <td id="r2c11" class="cell">.</td>
    <td id="r2c12" class="cell">.</td>
    <td id="r2c13" class="cell">.</td>
    <td id="r2c14" class="cell">.</td>
    <td id="r2c15" class="cell">.</td>
    <td id="r2c16" class="cell">.</td>
   </tr>
   <tr id="r3" class="row">
    <td id="r3c1" class="cell">.</td>
    <td id="r3c2" class="cell">.</td>
    <td id="r3c3" class="cell">.</td>
    <td id="r3c4" class="cell">.</td>
    <td id="r3c5" class="cell">.</td>
    <td id="r3c6" class="cell">.</td>
    <td id="r3c7" class="cell">.</td>
    <td id="r3c8" class="cell">.</td>
    <td id="r3c9" class="cell">.</td>
    <td id="r3c10" class="cell">.</td>
    <td id="r3c11" class="cell">.</td>
    <td id="r3c12" class="cell">.</td>
    <td id="r3c13" class="cell">.</td>
    <td id="r3c14" class="cell">.</td>
    <td id="r3c15" class="cell">.</td>
    <td id="r3c16" class="cell">.</td>
   </tr>
   <tr id="r4" class="row">
    <td id="r4c1" class="cell">.</td>
    <td id="r4c2" class="cell">.</td>
    <td id="r4c3" class="cell">.</td>
    <td id="r4c4" class="cell">.</td>
    <td id="r4c5" class="cell">.</td>
    <td id="r4c6" class="cell">.</td>
    <td id="r4c7" class="cell">.</td>
    <td id="r4c8" class="cell">.</td>
    <td id="r4c9" class="cell">.</td>
    <td id="r4c10" class="cell">.</td>
    <td id="r4c11" class="cell">.</td>
    <td id="r4c12" class="cell">.</td>
    <td id="r4c13" class="cell">.</td>
    <td id="r4c14" class="cell">.</td>
    <td id="r4c15" class="cell">.</td>
    <td id="r4c16" class="cell">.</td>
   </tr>
   <tr id="r5" class="row">
    <td id="r5c1" class="cell">.</td>
    <td id="r5c2" class="cell">.</td>
    <td id="r5c3" class="cell">.</td>
    <td id="r5c4" class="cell">.</td>
    <td id="r5c5" class="cell">.</td>
    <td id="r5c6" class="cell">.</td>
    <td id="r5c7" class="cell">.</td>
    <td id="r5c8" class="cell">.</td>
    <td id="r5c9" class="cell">.</td>
    <td id="r5c10" class="cell">.</td>
    <td id="r5c11" class="cell">.</td>
    <td id="r5c12" class="cell">.</td>
    <td id="r5c13" class="cell">.</td>
    <td id="r5c14" class="cell">.</td>
    <td id="r5c15" class="cell">.</td>
    <td id="r5c16" class="cell">.</td>
   </tr>
   <tr id="r6" class="row">
    <td id="r6c1" class="cell">.</td>
    <td id="r6c2" class="cell">.</td>
    <td id="r6c3" class="cell">.</td>
    <td id="r6c4" class="cell">.</td>
    <td id="r6c5" class="cell">.</td>
    <td id="r6c6" class="cell">.</td>
    <td id="r6c7" class="cell">.</td>
    <td id="r6c8" class="cell">.</td>
    <td id="r6c9" class="cell">.</td>
    <td id="r6c10" class="cell">.</td>
    <td id="r6c11" class="cell">.</td>
    <td id="r6c12" class="cell">.</td>
    <td id="r6c13" class="cell">.</td>
    <td id="r6c14" class="cell">.</td>
    <td id="r6c15" class="cell">.</td>
    <td id="r6c16" class="cell">.</td>
   </tr>
   <tr id="r7" class="row">
    <td id="r7c1" class="cell">.</td>
    <td id="r7c2" class="cell">.</td>
    <td id="r7c3" class="cell">.</td>
    <td id="r7c4" class="cell">.</td>
    <td id="r7c5" class="cell">.</td>
    <td id="r7c6" class="cell">.</td>
    <td id="r7c7" class="cell">.</td>
    <td id="r7c8" class="cell">.</td>
    <td id="r7c9" class="cell">.</td>
    <td id="r7c10" class="cell">.</td>
    <td id="r7c11" class="cell">.</td>
    <td id="r7c12" class="cell">.</td>
    <td id="r7c13" class="cell">.</td>
    <td id="r7c14" class="cell">.</td>
    <td id="r7c15" class="cell">.</td>
    <td id="r7c16" class="cell">.</td>
   </tr>
   <tr id="r8" class="row">
    <td id="r8c1" class="cell">.</td>
    <td id="r8c2" class="cell">.</td>
    <td id="r8c3" class="cell">.</td>
    <td id="r8c4" class="cell">.</td>
    <td id="r8c5" class="cell">.</td>
    <td id="r8c6" class="cell">.</td>
    <td id="r8c7" class="cell">.</td>
    <td id="r8c8" class="cell">.</td>
    <td id="r8c9" class="cell">.</td>
    <td id="r8c10" class="cell">.</td>
    <td id="r8c11" class="cell">.</td>
    <td id="r8c12" class="cell">.</td>
    <td id="r8c13" class="cell">.</td>
    <td id="r8c14" class="cell">.</td>
    <td id="r8c15" class="cell">.</td>
    <td id="r8c16" class="cell">.</td>
   </tr>
   <tr id="r9" class="row">
    <td id="r9c1" class="cell">.</td>
    <td id="r9c2" class="cell">.</td>
    <td id="r9c3" class="cell">.</td>
    <td id="r9c4" class="cell">.</td>
    <td id="r9c5" class="cell">.</td>
    <td id="r9c6" class="cell">.</td>
    <td id="r9c7" class="cell">.</td>
    <td id="r9c8" class="cell">.</td>
    <td id="r9c9" class="cell">.</td>
    <td id="r9c10" class="cell">.</td>
    <td id="r9c11" class="cell">.</td>
    <td id="r9c12" class="cell">.</td>
    <td id="r9c13" class="cell">.</td>
    <td id="r9c14" class="cell">.</td>
    <td id="r9c15" class="cell">.</td>
    <td id="r9c16" class="cell">.</td>
   </tr>
   <tr id="r10" class="row">
    <td id="r10c1" class="cell">.</td>
    <td id="r10c2" class="cell">.</td>
    <td id="r10c3" class="cell">.</td>
    <td id="r10c4" class="cell">.</td>
    <td id="r10c5" class="cell">.</td>
    <td id="r10c6" class="cell">.</td>
    <td id="r10c7" class="cell">.</td>
    <td id="r10c8" class="cell">.</td>
    <td id="r10c9" class="cell">.</td>
    <td id="r10c10" class="cell">.</td>
    <td id="r10c11" class="cell">.</td>
    <td id="r10c12" class="cell">.</td>
    <td id="r10c13" class="cell">.</td>
    <td id="r10c14" class="cell">.</td>
    <td id="r10c15" class="cell">.</td>
    <td id="r10c16" class="cell">.</td>
   </tr>
   <tr id="r11" class="row">
    <td id="r11c1" class="cell">.</td>
    <td id="r11c2" class="cell">.</td>
    <td id="r11c3" class="cell">.</td>
    <td id="r11c4" class="cell">.</td>
    <td id="r11c5" class="cell">.</td>
    <td id="r11c6" class="cell">.</td>
    <td id="r11c7" class="cell">.</td>
    <td id="r11c8" class="cell">.</td>
    <td id="r11c9" class="cell">.</td>
    <td id="r11c10" class="cell">.</td>
    <td id="r11c11" class="cell">.</td>
    <td id="r11c12" class="cell">.</td>
    <td id="r11c13" class="cell">.</td>
    <td id="r11c14" class="cell">.</td>
    <td id="r11c15" class="cell">.</td>
    <td id="r11c16" class="cell">.</td>
   </tr>
   <tr id="r12" class="row">
    <td id="r12c1" class="cell">.</td>
    <td id="r12c2" class="cell">.</td>
    <td id="r12c3" class="cell">.</td>
    <td id="r12c4" class="cell">.</td>
    <td id="r12c5" class="cell">.</td>
    <td id="r12c6" class="cell">.</td>
    <td id="r12c7" class="cell">.</td>
    <td id="r12c8" class="cell">.</td>
    <td id="r12c9" class="cell">.</td>
    <td id="r12c10" class="cell">.</td>
    <td id="r12c11" class="cell">.</td>
    <td id="r12c12" class="cell">.</td>
    <td id="r12c13" class="cell">.</td>
    <td id="r12c14" class="cell">.</td>
    <td id="r12c15" class="cell">.</td>
    <td id="r12c16" class="cell">.</td>
   </tr>
   <tr id="r13" class="row">
    <td id="r13c1" class="cell">.</td>
    <td id="r13c2" class="cell">.</td>
    <td id="r13c3" class="cell">.</td>
    <td id="r13c4" class="cell">.</td>
    <td id="r13c5" class="cell">.</td>
    <td id="r13c6" class="cell">.</td>
    <td id="r13c7" class="cell">.</td>
    <td id="r13c8" class="cell">.</td>
    <td id="r13c9" class="cell">.</td>
    <td id="r13c10" class="cell">.</td>
    <td id="r13c11" class="cell">.</td>
    <td id="r13c12" class="cell">.</td>
    <td id="r13c13" class="cell">.</td>
    <td id="r13c14" class="cell">.</td>
    <td id="r13c15" class="cell">.</td>
    <td id="r13c16" class="cell">.</td>
   </tr>
   <tr id="r14" class="row">
    <td id="r14c1" class="cell">.</td>
    <td id="r14c2" class="cell">.</td>
    <td id="r14c3" class="cell">.</td>
    <td id="r14c4" class="cell">.</td>
    <td id="r14c5" class="cell">.</td>
    <td id="r14c6" class="cell">.</td>
    <td id="r14c7" class="cell">.</td>
    <td id="r14c8" class="cell">.</td>
    <td id="r14c9" class="cell">.</td>
    <td id="r14c10" class="cell">.</td>
    <td id="r14c11" class="cell">.</td>
    <td id="r14c12" class="cell">.</td>
    <td id="r14c13" class="cell">.</td>
    <td id="r14c14" class="cell">.</td>
    <td id="r14c15" class="cell">.</td>
    <td id="r14c16" class="cell">.</td>
   </tr>
   <tr id="r15" class="row">
    <td id="r15c1" class="cell">.</td>
    <td id="r15c2" class="cell">.</td>
    <td id="r15c3" class="cell">.</td>
    <td id="r15c4" class="cell">.</td>
    <td id="r15c5" class="cell">.</td>
    <td id="r15c6" class="cell">.</td>
    <td id="r15c7" class="cell">.</td>
    <td id="r15c8" class="cell">.</td>
    <td id="r15c9" class="cell">.</td>
    <td id="r15c10" class="cell">.</td>
    <td id="r15c11" class="cell">.</td>
    <td id="r15c12" class="cell">.</td>
    <td id="r15c13" class="cell">.</td>
    <td id="r15c14" class="cell">.</td>
    <td id="r15c15" class="cell">.</td>
    <td id="r15c16" class="cell">.</td>
   </tr>
   <tr id="r16" class="row">
    <td id="r16c1" class="cell">.</td>
    <td id="r16c2" class="cell">.</td>
    <td id="r16c3" class="cell">.</td>
    <td id="r16c4" class="cell">.</td>
    <td id="r16c5" class="cell">.</td>
    <td id="r16c6" class="cell">.</td>
    <td id="r16c7" class="cell">.</td>
    <td id="r16c8" class="cell">.</td>
    <td id="r16c9" class="cell">.</td>
    <td id="r16c10" class="cell">.</td>
    <td id="r16c11" class="cell">.</td>
    <td id="r16c12" class="cell">.</td>
    <td id="r16c13" class="cell">.</td>
    <td id="r16c14" class="cell">.</td>
    <td id="r16c15" class="cell">.</td>
    <td id="r16c16" class="cell">.</td>
   </tr>
  </table>
  <div id="gape">
   <img src="http://code.google.com/appengine/images/appengine-silver-120x30.gif" alt="Powered by Google App Engine" />
  </div>
  <script language="Javascript">
prepCells();
placeRobot("red");
  </script>
""")

#      dbUser = DBUser.gql("WHERE user = :1", user)

#      result = dbUser.fetch(1)

#      if result != None:
#        self.response.out.write(result.count())
#        self.response.out.write("Good")
#      else
#        self.response.out.write("Bad")

    self.response.out.write("""</body>
</html>""")
#    else:
#      self.redirect(users.create_login_url(self.request.uri))


def main():
  application = webapp.WSGIApplication(
    [('/', MainPage)],
    debug=True)
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == "__main__":
  main()
